import 'package:flutter/foundation.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:kxy_flutter_app/helper/http.dart';
import 'package:kxy_flutter_app/model/exam_question.dart';
import 'package:kxy_flutter_app/model/test_result.dart';
import 'package:more/tuple.dart';

class TestService {
  String userId;
  String testId;
  String recordId;

  TestService({@required this.testId, @required this.userId});

  Future<List<Question>> getSpecialQuestionList() async {
    // var prefs = await SharedPreferences.getInstance();
    return [];
  }

  Future<List<Question>> getQuestionList() async {
    var res = await Http.instance.post('/exam-app/daily/answer/answerQuesList', params: {
      'dailyId': testId,
      'userId': userId,
    });

    if (res.code == 200) {
      this.recordId = res.data['answRecordId'];
      return (res.data['quesBankList'] as List).map((item) {
        return Question.fromJson(item);
      }).toList();
    } else {
      Fluttertoast.showToast(msg: res.msg);
      return [];
    }
  }

  Future<Tuple2<bool, String>> submitAnswer(String quesId, String answer, int spendSecond) async {
    var res = await Http.instance.post('/exam-app/daily/answer/submit', params: {
      'answerRecordId': recordId,
      'quesId': quesId,
      'answer': answer,
      'userId': userId,
      'answerTime': spendSecond,
    });
    if (res.code == 200) {
      return new Tuple2(res.data['submit'] as bool, res.data['rightAnswer'] as String);
    } else {
      Fluttertoast.showToast(msg: res.msg);
      return new Tuple2(false, '');
    }
  }

  Future<TestResult> getTestResult() async {
    var res = await Http.instance.post('/exam-app/daily/answer/result', params: {
      'answerRecordId': recordId,
      'userId': userId,
    });

    if (res.code == 200) {
      return TestResult.fromJson(res.data);
    } else {
      Fluttertoast.showToast(msg: res.msg);
    }

    return null;
  }

  Future collectQuestion(String quesId) async {
    var res = await Http.instance.post('/exam-app/coll/collQues', params: {
      'quesId': quesId,
      'userId': userId,
    });

    if (res.code != 200) {
      Fluttertoast.showToast(msg: res.msg);
    }
  }

  Future cancelCollectQuestion(String quesId) async {
    var res = await Http.instance.post('/exam-app/coll/unCollQues', params: {
      'quesId': quesId,
      'userId': userId,
    });

    if (res.code != 200) {
      Fluttertoast.showToast(msg: res.msg);
    }
  }
}
